<?php
//
defined('BY_JYA') or exit('error');
//cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$order_types = array(
		'1'=>'堂点',
		'2'=>'外卖',
		'3'=>'自取',
);
if($_RQ['op']=='main'){
	if(intval($_RQ['store_id'])){
		$store_id = intval($_RQ['store_id']);
		$this->session('store_id',$store_id);
	}
	if(!$store_id){
		$store_id=$this->session('store_id');
	}
	if(!$store_id){
		exi('请先选择门店！','error','jy_meye/index/');
	}
	$order_type=$this->session('order_type');
	$carts=$this->session('carts');
	$goodsprice = 0;
	if($carts){
		foreach($carts as $k=>&$v){
			$allnum += $v['num'];
			$goodinfo = pdo_get('jy_meye_goods',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$k));
			$v['packageinfo']=$goodinfo['package']?json_decode($goodinfo['package'],true):array();
			if(!empty($v['packageinfo'])){
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$goodinfo['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
					$goodsprice += $p['num']*$p['price'];
				}	
			}else{
				$goodsprice += $v['num']*$goodinfo['price'];
			}
			$v['goodname']=$goodinfo['title'];
			$v['goodprice']=$goodinfo['price'];
			$v['goodnum']=$v['num'];
		}
	}
	$store=pdo_get('jy_meye_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	$youhui = 0;
	if($order_type == 1){
		$table_id=$this->session('table_id');
		$return['table_id']=$table_id;
		$allprice = $goodsprice - $youhui;
	}elseif($order_type == 2){
		if($goodsprice >= $store['out_end']){
			$youhui = $store['out_pay'];
		}
		$addressinfo=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
		$addressinfo['pcd']=json_decode($addressinfo['pcd'],true);
		$return['addressinfo']=$addressinfo;
		$out_time = out_times($store_id);
		$timestamp = TIMESTAMP + 60 * 30;
		$data = array_order($timestamp, $out_time['timestamp']);
		$predict_index = array_search($data, $out_time['timestamp']);
		$predict_day = $out_time['days'][0];
		$predict_time = $out_time['times'][$predict_index]['start'].'~'.$out_time['times'][$predict_index]['end'];
		$text_time = "尽快送达";
		$return['timestamp']=$timestamp;
		$return['predict_time']=$predict_time;
		$return['out_time']=$out_time;
		$allprice = $goodsprice + $store['out_pay'] - $youhui;
	}elseif($order_type == 3){
		$allprice = $goodsprice - $youhui;
	}elseif($order_type == 5){
		$tables = pdo_getall('jy_meye_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('title','id'));
		foreach($tables as &$t){
			$t['table_list']=pdo_getall('jy_meye_table',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1,'group_id'=>$t['id']));
		}
		$return['tables']=$tables;
		$allprice = $goodsprice - $youhui;
	}
	$return['order_typename']=$order_types[$order_type];
	$return['order_type']=$order_type;
	$return['carts']=$carts;
	$return['store']=$store;
	$return['allprice']=$allprice;
	$return['youhui']=$youhui;
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='selecttable'){
	$store_id=$this->session('store_id');
	$return['tables']=pdo_getall('jy_meye_table',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1,'group_id'=>$_RQ['group_id']));
	$return['success']=1;
	exi($return);
}
	//提交订单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择门店！','error','jy_meye/index/');
	}	
	$store=pdo_get('jy_meye_store',array('id'=>$store_id));
	$youhui = 0;
	$carts=$this->session('carts');
	if(!$carts){
		exi('没有选择商品！','error');
	}
	$total=0;
	foreach($carts as $k=>&$v){
			$allnum += $v['num'];
			$goodinfo = pdo_get('jy_meye_goods',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$k));
			$v['packageinfo']=$goodinfo['package']?json_decode($goodinfo['package'],true):array();
			if(!empty($v['packageinfo'])){
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$goodinfo['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
					$total += $p['num']*$p['price'];
				}	
			}else{
				$total += $v['num']*$goodinfo['price'];
			}
	}
	if($this->session('order_type')==1){
		$table=pdo_get('jy_meye_table',array('id'=>$this->session('table_id')));
		if($table['busy']==1){
			exi('餐桌已被占用，请重新选择餐桌','error');
		}
		$price=$store['in_pay']*$post['user_num']+$total - $youhui;	
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'order_type'=>1,
			'ordersn'=>get_ordersn('jy_meye_order','ordersn'),
			'price'=>$price,
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_meye_order',$data);
		$this->session('insertid',pdo_insertid());
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'user_num'=>$post['user_num'],
			'table_id'=>$this->session('table_id'),
			'order_id'=>pdo_insertid()
		);
		pdo_insert('jy_meye_order_table',$data);
	}
	if($this->session('order_type')==2){
		$lng=json_decode($store['lat_lng'],true);
		$out_distance=$store['out_distance'];
		$user_lng=$post['receiver_latng'];
		if($user_lng){
			$distance=cfc('tools')->getdistance($lng['lng'],$lng['lat'],$user_lng['lng'],$user_lng['lat']);
			if($distance>$out_distance){
				exi('超出配送距离','error');
			}
		}
		if($store['out_end']<$total){
			$price=$total - $youhui;
		}else{
			$price=$total+$store['out_pay'] -$youhui;	
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'order_type'=>2,
			'ordersn'=>get_ordersn('jy_meye_order','ordersn'),
			'price'=>$price,
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_meye_order',$data);
		$this->session('insertid',pdo_insertid());
		$addressinfo=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'order_id'=>pdo_insertid(),
			'receiver_name'=>$addressinfo['name'],
			'receiver_mobile'=>$addressinfo['mobile'],
			'receiver_address'=>$addressinfo['pcd'],
			'receiver_latng'=>$post['receiver_latng'],
			'out_time'=>ijson_encode($post['out_time']),
		);
		$addressinfo['pcd']=json_decode($addressinfo['pcd'],true);
		pdo_insert('jy_meye_order_out',$data);
	}
	if($this->session('order_type')==3){
		$price=$total - $youhui;
		$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'mid'=>$_SESSION['uid'],
		'order_type'=>3,
		'ordersn'=>get_ordersn('jy_meye_order','ordersn'),
		'price'=>$price,
		'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_meye_order',$data);
		$this->session('insertid',pdo_insertid());
	}
	$content1 = "名称　             数量    单价\n";
	foreach($carts as $k=>&$v){
			$str='';
			$good=pdo_get('jy_meye_goods',array('id'=>$k));
			$v['packageinfo']=$good['package']?json_decode($good['package'],true):array();
			if(!empty($v['packageinfo'])){
				$str .= $good['title'].'--------------\n';
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$good['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}else{
						$money = $p['price'] * $p['num'];
						$str .= '----' . str_pad($p['title'], '18', ' ', STR_PAD_RIGHT);
						$str .= ''.str_pad($p['num'], '5', ' ', STR_PAD_RIGHT);
						$str .= ''. number_format($money, 2) . '元\n';
					}
				}
			}else{
				$money = $good['price'] * $v['num'];
				$str .= ''.str_pad($good['title'], '23', ' ', STR_PAD_RIGHT);
				$str .= ''.str_pad($v['num'], '5', ' ', STR_PAD_RIGHT);
				$str .= ''. number_format($money, 2) . '元\n';
			}
			$content1 .=$str;
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'store_id'=>$store_id,
				'order_id'=>$this->session('insertid'),
				'goods_id'=>$k,
				'goods_num'=>$v['num'],
				'goods_price'=>$good['price'],			
				'goods_title'=>$good['title'],
				'goods_package'=>ijson_encode($v['packageinfo']),
			);
			pdo_insert('jy_meye_order_goods',$data);
	}
	$content1 .= "-------------------------------\n";
	$this->session('carts','');
	$nickname=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname');
	$order_typek = $this->session('order_type');
	$order_type=$this->type__order();
	$order_type=$order_type[$order_typek];
	$tpldata=array(
		'first'=>array('value'=>'订单下单通知','color'=>''),
		'tradeDateTime'=>array('value'=>'下单时间:'.date('Y-m-d h:i:s',TIMESTAMP),'color'=>'#ff510'),
		'orderType'=>array('value'=>'订单类型:'.$order_type,'color'=>'#ff510'),
		'customerInfo'=>array('value'=>'下单人'.$nickname,'color'=>'#ff510'),
		'orderItemName'=>array('value'=>'商品详情','color'=>'#ff510'),
		'orderItemData'=>array('value'=>'商品详情内容\n'.$content1,'color'=>'#ff510'),
		'remark'=>array('value'=>'其他信息','color'=>'#ff510'),
	);
	$url=custom_url('jy_meye/order_show/',true);
	$text = '订单下单通知\n';
	$text .='下单时间:'.date('Y-m-d h:i:s',TIMESTAMP).'\n';
	$text .='订单类型:'.$order_type.'\n';
	$text .='下单人:'.$nickname.'\n';
	$text .='【商品详情】\n';
	$text .='--------------------\n';
	$text .=$content1.'\n';
	//发信息
	$this->notice('order_new',$text,$tpldata,$_SESSION['uid'],$url,'#980000');
	//打印
	$this->printmsg($text,$order_type,$store_id);
    $params['fee']     = $price;
    $params['title']   = $order_type.'订单';
	$params['id']=$this->session('insertid');
	$params['plugin']='jy_meye';
	$params['back_url']='jy_meye/order/payresult';
	cfc('core')->pay($params);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];
	
	if($payresult['status']==1){
		$order=pdo_get('jy_meye_order',array('id'=>$payresult['id']));
		if($order['price']==$payresult['fee']){
			pdo_update('jy_meye_order',array('pay_type'=>$payresult['pay_type'],'is_pay'=>1,'pay_id'=>$payresult['pay_id']),array('id'=>$payresult['id']));
			$goodinfo=pdo_getall('jy_meye_order_goods',array('order_id'=>$order['id']));
			$content1 = "名称　             数量    单价\n";
			foreach($goodinfo as $g){
				$str = '';
				if(!empty($g['goods_package'])){
					$str .= $g['goods_title'].'--------------\n';
					foreach($g['goods_package'] as $k=>$p){
						$money = $p['price'] * $p['num'];
						$str .= '----' . str_pad($p['title'], '18', ' ', STR_PAD_RIGHT);
						$str .= ''.str_pad($p['num'], '5', ' ', STR_PAD_RIGHT);
						$str .= ''. number_format($money, 2) . '元\n';
					}
				}else{
					$money = $g['goods_price'] * $p['goods_num'];
					$str .= '' . str_pad($g['goods_title'], '23', ' ', STR_PAD_RIGHT);
					$str .= ''.str_pad($g['goods_num'], '5', ' ', STR_PAD_RIGHT);
					$str .= ''. number_format($money, 2) . '元\n';
				}
					$content1 .=$str;
			}
			$content1 .= "-------------------------------\n";
			$url=custom_url('jy_meye/order_show/',true);
			$store=pdo_getcolumn('jy_meye_store',array('id'=>$order['store_id']),'title');
			$tpldata=array(
				'first'=>array('value'=>'订单付款通知','color'=>''),
				'keyword1'=>array('value'=>'订单编号:'.$payresult['id'],'color'=>'#ff510'),
				'keyword2'=>array('value'=>'消费金额:'.$payresult['fee'],'color'=>'#ff510'),
				'keyword3'=>array('value'=>'消费门店:'.$store,'color'=>'#ff510'),
				'keyword4'=>array('value'=>'消费时间:'.$order['createtime'],'color'=>'#ff510'),
				'remark'=>array('value'=>'更多消息请查看订单详情','color'=>'#ff510'),
			);
			$text = '订单付款通知\n';
			$text .='订单编号:'.$payresult['id'].'\n';
			$text .='消费金额:'.$payresult['fee'].'\n';
			$text .='消费门店:'.$store.'\n';
			$text .='消费时间:'.$order['createtime'].'\n';
			$text .='【商品详情】\n';
			$text .='-------------------------------\n';
			$text .=$content1;
			//发信息
			$this->notice('order_pay',$text,$tpldata,$_SESSION['uid'],$url,'#980000');
			//打印
			$this->printmsg($text,$order['order_type'],$order['store_id']);
			exi('支付成功!','success','jy_meye/order_show/');
		}else{
			exi('支付失败！','error','jy_meye/order/');
		}
	}else{
		exi('支付失败。','error','jy_meye/order/');
	}
}
?>